package com.ohuang.launcher.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DialogUtil {

    public static void showDialog(Context context, View.OnClickListener clickListener, View view, String title, String msg) {
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle(title)//设置对话框的标题
                .setMessage(msg)//设置对话框的内容
                //设置对话框的按钮
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        clickListener.onClick(view);
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();

    }

    public static void showDialogNotCancel(Context context, View.OnClickListener clickListener, View view, String title, String msg) {
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle(title)//设置对话框的标题
                .setMessage(msg)//设置对话框的内容
                //设置对话框的按钮
                .setCancelable(false)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        clickListener.onClick(view);
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();

    }

    public static void showDialogNotCancel(Activity context,Object o, String methodName,  String title, String msg) {
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle(title)//设置对话框的标题
                .setMessage(msg)//设置对话框的内容
                //设置对话框的按钮
                .setCancelable(false)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            Method declaredMethod = o.getClass().getDeclaredMethod(methodName);
                            declaredMethod.invoke(o);
                        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();

    }
}
